package com.cheneyjoo.controller;

import com.cheneyjoo.factory.ClassFactory;
import com.cheneyjoo.model.STUDENT;
import com.cheneyjoo.service.IClassStudentsService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class ClassStudentsController {
    private final IClassStudentsService classStudentsService;

    public ClassStudentsController() {
        classStudentsService = ClassFactory.getClassStudentsService();
    }

    /**
     * 获取没有班级的学生
     *
     * @return 未分配班级的学生列表
     */
    public List<STUDENT> getNoClassStudents() {
        return classStudentsService.getNoClassStudents();
    }

    /**
     * 分配学生
     *
     * @param studentIds 学生ID列表
     * @param classId    班级ID
     * @return 是否成功
     */
    public boolean distributionStudentClass(String studentIds, int classId) {
        AtomicReference<String> newIds = new AtomicReference<>("");
        Arrays.stream(studentIds.split(",")).map(s -> "'" + s + "'").forEach(s -> {
            newIds.set(newIds.get() + s + ",");
        });
        studentIds = newIds.get().substring(0, newIds.get().length() - 1);
        return classStudentsService.distributionStudentClass(studentIds, classId);
    }

    /**
     * 移除班级学生
     *
     * @param studentIds 学生列表
     * @return 是否成功
     */
    public boolean removeClassStudent(String studentIds,int classId) {
        AtomicReference<String> newIds = new AtomicReference<>("");
        Arrays.stream(studentIds.split(",")).map(s -> "'" + s + "'").forEach(s -> {
            newIds.set(newIds.get() + s + ",");
        });
        studentIds = newIds.get().substring(0, newIds.get().length() - 1);
        return classStudentsService.removeClassStudent(studentIds,classId);
    }

    /**
     * 获取指定班级的学生
     *
     * @return 班级学生列表
     */
    public Map<String, Object> getClassStudents(int pageSize, int currPage, Map<String, String> where) {
        return classStudentsService.getClassStudents( pageSize,  currPage, where);
    }
}
